<?php

class Empeval_Methods_Request_SetBi {
	
	/**
	 *
	 * @var App_Methods_Data_App
	 */
	public $app;
	public $bonusGoalsFormId;
	public $behavioralIndicatorId;
	public $evaluationId;
	public $userId;
	
	public function __construct($params) {
		$this->app = new App_Methods_Data_App($params->app);
		$this->bonusGoalsFormId = $params->bonusGoalsFormId;
		$this->behavioralIndicatorId = $params->behavioralIndicatorId;
		$this->evaluationId = $params->evaluationId;
		$this->userId = $params->userId;
	}
	
	public function process() {
		$evaluation = EmpevalEmployeeEvaluationsQuery::create()
		->filterByUserId($this->userId)
		->filterByBonusGoalsFormId($this->bonusGoalsFormId)
		->findOne();
		
		if (!($evaluation instanceof EmpevalEmployeeEvaluations))
			throw new Empeval_Exception_App(Empeval_Exception_Codes::INFO_ACCESS_DENIED);
		
		$con = Propel::getConnection('vgp_empeval');
		$con->beginTransaction();
		try {
			$updatedCount = EmpevalEmployeeEvaluationValuesQuery::create()
			->filterByBonusGoalsFormId($this->bonusGoalsFormId)
			->filterByBehavioralIndicatorId($this->behavioralIndicatorId)
			->update(array('EvaluationId' => $this->evaluationId), $con);
			if ($updatedCount == 0) {
				$value = new EmpevalEmployeeEvaluationValues();
				$value->setBonusGoalsFormId($this->bonusGoalsFormId);
				$value->setBehavioralIndicatorId($this->behavioralIndicatorId);
				$value->setEvaluationId($this->evaluationId);
				$value->save($con);
			}
			$con->commit();
		} catch (Exception $e) {
			$con->rollBack();
			throw $e;
		}
		
		return new Empeval_Methods_Response_SetBi(true);
	}
	
}